﻿//using Net.WinComponent;
using Net.Orm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Net.Studio.Forms
{
    public class DBModel
    {
        private string _No;

        private Dictionary<string, TableModel> _Tables = new Dictionary<string, TableModel>();

        string _FileName = string.Empty;

        public string No
        {
            get
            {
                if (string.IsNullOrEmpty(_No))
                    _No = "PhysicalDataModel_1";
                return _No;
            }

            set
            {
                _No = value;
            }
        }

        /// <summary>
        /// 表
        /// </summary>
        public Dictionary<string, TableModel> Tables
        {
            get { return _Tables; }
            set { _Tables = value; }
        }

        public string FileName
        {
            get
            {

                return _FileName;
            }

            set
            {
                _FileName = value;
            }
        }


    }

    public class TableModel
    {
        public TableModel() { }
        public TableModel(string tableDescribe)
        {
            string[] arrTable = null;
            if (tableDescribe.IndexOf(",") > 0)
            {
                arrTable = tableDescribe.Split(new char[] { ',' });
            }
            this.No = arrTable[0].Trim();
            this.Name = arrTable[1].Trim(); 
        }

        string _No;
        /// <summary>
        /// 实体编号
        /// </summary>
        public string No
        {
            get { return _No; ; }
            set { _No = value; }
        }

        string _Name = string.Empty;
        /// <summary>
        /// 实体名称
        /// </summary>
        public string Name
        {
            get { return _Name; }
            set
            {
                int aBracketIndex = value.IndexOf("[");
                int bBracketIndex = value.IndexOf("]");
                if (aBracketIndex >= 0 && bBracketIndex >= 0)
                {
                    _Name = value.Substring(aBracketIndex + 1, bBracketIndex - aBracketIndex - 1);
                }
                else
                {
                    _Name = value;
                }
            }
        }

        public int ImageIndex = -1;
        //public StudioNodeTypes kind = StudioNodeTypes.None;

        private SQLExcuteType sqlexcutetype;
        public SQLExcuteType sqlExcuteType
        {
            get { return sqlexcutetype; }
            set { sqlexcutetype = value; }
        }

        private string strsql;
        public string StrSQL
        {
            get
            {
                if (sqlexcutetype == SQLExcuteType.NO) return string.Empty;
                return strsql;
            }
            set { strsql = value; }
        } 

        public Dictionary<string, TableModelField> Fields = new Dictionary<string, TableModelField>();
        public Dictionary<string, TableModelRelation> Relations = new Dictionary<string, TableModelRelation>();


        public string ToQuery()
        {
            return this.StrSQL;
        }
    }

    public class TableModelField
    {
        private int sortNo = 0;
        private string id = string.Empty;
        private string dTFNo = string.Empty;
        private string dTFName = string.Empty;
        private string fullDTFType = string.Empty;
        private string dataLength = string.Empty;
        private string decLength = string.Empty;
        private string defaultValue = string.Empty;
        private string isNotNull = string.Empty;
        private string indentKey = string.Empty;
        private string primaryKey = string.Empty;
         
         
        public int SortNo
        {
            get
            {
                return sortNo;
            }

            set
            {
                sortNo = value;
            }
        }

        public string Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        public string DTFNo
        {
            get
            {
                return dTFNo;
            }

            set
            {
                dTFNo = value;
            }
        }

        public string DTFName
        {
            get
            {
                return dTFName;
            }

            set
            {
                int aBracketIndex = value.IndexOf("[");
                int bBracketIndex = value.IndexOf("]");
                if (aBracketIndex >= 0 && bBracketIndex >= 0)
                {
                    this.dTFName = value.Substring(aBracketIndex + 1, bBracketIndex - aBracketIndex - 1);
                }
                else
                {
                    this.dTFName = value;
                }
            }
        }

        string dTFType = string.Empty;
        public string DTFType
        {
            get
            {
                if (string.IsNullOrEmpty(dTFType))
                {
                    int aBracketIndex = FullDTFType.IndexOf("(");
                    if (aBracketIndex > 0)
                    {
                        dTFType = FullDTFType.Substring(0, aBracketIndex);
                    }
                    else
                    {
                        dTFType = FullDTFType;
                    }
                }
                return dTFType;
            }
        }

        public string FullDTFType
        {
            get
            {
                return fullDTFType;
            }

            set
            {
                fullDTFType = value;
            }
        }

        public string DataLength
        {
            get
            {
                return dataLength;
            }

            set
            {
                dataLength = value;
            }
        }

        public string DecLength
        {
            get
            {
                return decLength;
            }

            set
            {
                decLength = value;
            }
        }

        public string DefaultValue
        {
            get
            {
                return defaultValue;
            }

            set
            {
                defaultValue = value;
            }
        }

        public string IsNotNull
        {
            get
            {
                return isNotNull;
            }

            set
            {
                isNotNull = value;
            }
        }

        public string IndentKey
        {
            get
            {
                return indentKey;
            }

            set
            {
                indentKey = value;
            }
        }
        //

        public string PrimaryKey
        {
            get
            {
                return primaryKey;
            }

            set
            {
                primaryKey = value;
            }
        } 
    }

    public class TableModelRelation
    {
        private int sortNo = 1;
        private string id = string.Empty;
        private string dTRNo = string.Empty;
        private string dTRType = string.Empty;
        private string filedNos = string.Empty;
        private string fKFiledNos = string.Empty; 

        public int SortNo
        {
            get
            {
                return sortNo;
            }

            set
            {
                sortNo = value;
            }
        }

        public string Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        public string DTRNo
        {
            get
            {
                return dTRNo;
            }

            set
            {
                dTRNo = value;
            }
        }

        public string DTRType
        {
            get
            {
                return dTRType;
            }

            set
            {
                dTRType = value;
            }
        }

        public string FiledNos
        {
            get
            {
                return filedNos;
            }

            set
            {
                filedNos = value;
            }
        }

        public string FKFiledNos
        {
            get
            {
                return fKFiledNos;
            }

            set
            {
                fKFiledNos = value;
            }
        } 
    }

    public static class DMEntityExtensions
    {
        public static Dictionary<string, TableModel> Sort(this Dictionary<string, TableModel> dic)
        {
            Dictionary<string, TableModel> dicSort = dic.OrderBy(o => o.Key).ToDictionary(p => p.Key, o => o.Value);
            return dicSort;
        }


        public static List<TableModelField> ToList(this Dictionary<string, TableModelField> fields)
        {
            List<TableModelField> arrFields = new List<TableModelField>();
            foreach (KeyValuePair<string, TableModelField> kvp in fields)
            {
                arrFields.Add(kvp.Value);
            }
            return arrFields;
        }

        public static List<TableModelRelation> ToList(this Dictionary<string, TableModelRelation> relations)
        {
            List<TableModelRelation> arrRelations = new List<TableModelRelation>();
            foreach (KeyValuePair<string, TableModelRelation> kvp in relations)
            {
                arrRelations.Add(kvp.Value);
            }
            return arrRelations;
        }
    }

    public enum SQLExcuteType
    {
        NO = 0,
        Create=1,
        Alter=2,
        Drop=3
    } 
}
